/**
 * *************************************
 * Copyright(c)2016 Dingdang Yixia 
 * All Rights Reserved.
 *
 * @File: WdgjStockBaseService.java
 * @Package: com.wdgj.sdk.service.base
 * @Description:
 * 
 * @Version: V1.0
 * @Author: Yidong.Xiang
 * @Date: 2018年9月6日 下午9:57:38
 * 
 * *************************************
 */
package com.wdgj.sdk.service.base;

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.wdgj.sdk.model.domain.StockOrder;
import com.wdgj.sdk.model.domain.StockGoods;
import com.wdgj.sdk.model.request.SyncWdgjStockRequest;
import com.wdgj.sdk.util.WdgjCloudClient;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: WdgjStockBaseService.java
 * @Module: 
 * @Description: 
 * 
 * @author: Yidong.Xiang
 * @date: 2018年9月6日 下午9:57:38
 * 
 */
@Slf4j
public abstract class WdgjStockBaseService{

	/**
	 * 	网店管家库存异动接口
	 * @param stockOrderNo	出库单号或入库单号
	 * @param warehouseNo	仓库编号
	 * @param operator		出库或入库操作员
	 * @param goodsNo		商品编码
	 * @param specName		规格名称
	 * @param goodsQty		出入库数量
	 * @param type			出入库标识（in表示入库,out表示出库，默认为in）
	 * @return				是否同步成功
	 */
	public boolean synchronize(String stockOrderNo, String warehouseNo, String operator, String goodsNo, String specName,Integer goodsQty,String type) {
		boolean result = false;
		String response = "";
		try {	
			//创建商品库存数
			StockGoods goods = new StockGoods();
			goods.setGoodsno(goodsNo);
			goods.setSpecname(specName);
			if("out".equalsIgnoreCase(type))
				goodsQty = 0 - goodsQty;
			goods.setGoodscount(Integer.toString(goodsQty));
			//创建库存清单对象
			List<StockGoods> goodsList = new ArrayList<StockGoods>();
			goodsList.add(goods);
			//创建店铺出库操作对象
			StockOrder stockOrder =  new StockOrder();
			stockOrder.setWarehouseno(warehouseNo);
			stockOrder.setTradeno(stockOrderNo);
			stockOrder.setOperater(operator);
			stockOrder.setGoodslist(goodsList);
			//创建同步库存接口请求对象
			SyncWdgjStockRequest request = new SyncWdgjStockRequest(stockOrder);
			response = WdgjCloudClient.sendPost(request);
			//根据网店管家OpenAPI返回JSON格式字符串判断是否同步成功
			JSONObject jo = JSONObject.parseObject(response);
			if(jo.containsKey("returncode"))
				if("0".equals(jo.getString("returncode")))
					result=true;
		}
		catch(Exception e)
		{
			log.error("[网店管家库存异动接口异常]",response);
			return false;
		}
		return result;
	}
	
	public boolean synchronize(String stockOrderNo, String warehouseNo, String operator, Integer goodsId,String goodsCode, String specName,Integer goodsQty,String type) {
		if(goodsCode==null || "".equals(goodsCode))
			goodsCode = "0";
		StringBuffer goodsNo = new StringBuffer("").append(goodsId).append("~").append(goodsCode);
		return synchronize(stockOrderNo,warehouseNo,operator,goodsNo.toString(),specName,goodsQty,type);
	}

}
